package com.example.shuiyin.controller;

import com.example.shuiyin.dto.ApiResponse;
import com.example.shuiyin.service.MinioStorageService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/admin")
@RequiredArgsConstructor
@Slf4j
public class AdminController {

    private final MinioStorageService minioStorageService;

    /**
     * 清理测试文件
     * @param prefix 可选的前缀，默认清理所有测试文件
     * @return 清理结果
     */
    @PostMapping("/cleanup-test-files")
    @PreAuthorize("hasRole('ADMIN')")
    public ResponseEntity<ApiResponse<Integer>> cleanupTestFiles(
            @RequestParam(required = false) String prefix) {
        
        log.info("开始清理测试文件，前缀: {}", prefix);
        int deletedCount;
        
        if (prefix != null && !prefix.isEmpty()) {
            deletedCount = minioStorageService.cleanupTestFiles(prefix);
        } else {
            deletedCount = minioStorageService.cleanupAllTestFiles();
        }
        
        return ResponseEntity.ok(ApiResponse.success("成功清理测试文件", deletedCount));
    }
} 